{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchvision import transforms, datasets\n",
    "import torch\n",
    "import torchvision\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import dataset, dataloader\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    height,weight=53,53\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(1, 6, 5)\n",
    "        self.batch1 = nn.BatchNorm2d(6)\n",
    "        self.conv2 = nn.Conv2d(6,16, 5)\n",
    "        self.batch2 = nn.BatchNorm2d(16)\n",
    "#         self.upsample=nn.ConvTranspose2d(1, 1,20)\n",
    "        self.fc1 = nn.Linear(1016064, 1000)\n",
    "        self.fc2 = nn.Linear(1000, 120)\n",
    "        self.fc3 = nn.Linear(120, 84)\n",
    "        self.fc4 = nn.Linear(84, 2)\n",
    "        \n",
    "    def forward(self, x):\n",
    "\n",
    "        x = F.relu(self.batch1(self.conv1(x)))\n",
    "        x = F.relu(self.batch2(self.conv2(x)))\n",
    "        a=x.cpu()\n",
    "        plt.imshow(a[0][0].detach().numpy())\n",
    "        plt.show()\n",
    "        \n",
    "        x = x.view(-1, self.num_flat_features(x))\n",
    "        \n",
    "        x = F.relu(self.fc1(x))\n",
    "        x = F.relu(self.fc2(x))\n",
    "        x = F.relu(self.fc3(x))\n",
    "        x = self.fc4(x)\n",
    "\n",
    "        return x\n",
    "\n",
    "    def num_flat_features(self, x):\n",
    "        size = x.size()[1:]  # all dimensions except the batch dimension\n",
    "        num_features = 1\n",
    "        for s in size:\n",
    "            num_features *= s\n",
    "        return num_features\n",
    "    \n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "model = Net().to(device)\n",
    "print(model)\n",
    "# import torch\n",
    "# print(torch.cuda.is_available())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "criterion = nn.CrossEntropyLoss().to(device)\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_transform = transforms.Compose([\n",
    "#         transforms.Resize(176),\n",
    "        transforms.ToTensor()\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train_data = torchvision.datasets.ImageFolder(\"D:\\\\Projects\\\\Alzheimer's Research\\\\data1\\\\train\", transform=data_transform)\n",
    "test_data = torchvision.datasets.ImageFolder(\"resized\\\\\", transform=data_transform)\n",
    "# train_loader = torch.utils.data.DataLoader(train_data, batch_size=1, shuffle = True, num_workers=4)\n",
    "test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, shuffle = True, num_workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train_data = torchvision.datasets.ImageFolder(\"D:\\\\Projects\\\\Alzheimer's Research\\\\data1\\\\train\", transform=data_transform)\n",
    "val_data = torchvision.datasets.ImageFolder(\"resized_val\", transform=data_transform)\n",
    "# train_loader = torch.utils.data.DataLoader(train_data, batch_size=1, shuffle = True, num_workers=4)\n",
    "val_loader = torch.utils.data.DataLoader(test_data, batch_size=1, shuffle = False, num_workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data.classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# net.load_state_dict(torch.load('RecogModelv1.pt'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_step = len(test_loader)\n",
    "epochs=1\n",
    "# net.train()\n",
    "for epoch in range(epochs):\n",
    "    total=0\n",
    "    running_loss = 0.0\n",
    "    for i, (inputs, labels) in enumerate(test_loader):\n",
    "#         inp = inputs[0][0][5:245,60:300]\n",
    "#         inp = inp.view(1, 1, 240, 240).to(device)\n",
    "        inputs=inputs.to(device)\n",
    "        labels = labels.to(device)\n",
    "        outputs = model(inputs[0][0].reshape(1,1,inputs.shape[2],inputs.shape[3]))\n",
    "#         break\n",
    "#         print(outputs.shape)\n",
    "        loss = criterion(outputs, labels)\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        running_loss += loss.item()\n",
    "        total=total+1\n",
    "        print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Total loss : {:.4f}'\n",
    "            .format(epoch+1,epochs,i+1,total_step, loss.item(), running_loss))\n",
    "    #         torch.save({'epoch': epoch + 1, 'state_dict': net.state_dict()},\n",
    "    #                    '{}/checkpoint_{}.pth'.format('nnState', epoch))\n",
    "    torch.save(model.state_dict(), '..\\\\Saved Model\\\\RecogModelv3.pt')\n",
    "\n",
    "    print(\"Total Loss : \" + str(running_loss/total))\n",
    "print('Finished Training')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "correct = 0\n",
    "total = 0\n",
    "pred=[]\n",
    "label=[]\n",
    "with torch.no_grad():\n",
    "    for images, labels in val_loader:\n",
    "        inputs=inputs.to(device)\n",
    "        labels = labels.to(device)\n",
    "        outputs = model(inputs[0][0].reshape(1,1,inputs.shape[2],inputs.shape[3]))\n",
    "        print(outputs.shape)\n",
    "        loss = criterion(outputs, labels)\n",
    "        print(loss.item())\n",
    "        _, predicted = torch.max(outputs.data, 1)\n",
    "        pred.append(predicted)\n",
    "        label.append(labels)\n",
    "#         print(predicted.shape)\n",
    "#         print(labels.shape)\n",
    "        total += labels.size(0)\n",
    "        correct += (predicted == labels).sum().item()\n",
    "        print('Accuracy of the test images: %d %%' % (100 * correct / total))\n",
    "        print(predicted,labels)\n",
    "\n",
    "print('Accuracy of the network on the test images: %d %%' % (\n",
    "    100 * correct / total))\n",
    "\n",
    "# print(total)\n",
    "# print(pred)\n",
    "# print(label)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(pred)\n",
    "len(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_step = len(val_loader)\n",
    "total = 0\n",
    "correct = 0\n",
    "\n",
    "with torch.no_grad():\n",
    "    for inputs, labels in test_loader:\n",
    "        inputs=inputs.to(device)\n",
    "        labels = labels.to(device)\n",
    "        outputs = net(inputs[0][0].reshape(1,1,inputs.shape[2],inputs.shape[3]))\n",
    "        _, predicted = torch.max(outputs.data, 1)\n",
    "#         print(predicted, labels)\n",
    "        total += labels.size(0)\n",
    "        if predicted == labels :\n",
    "            correct += 1\n",
    "# print('Accuracy of the system : {}/{}'.format(correct/total))\n",
    "#         print()\n",
    "#         optimizer.zero_grad()\n",
    "#         loss.backward()\n",
    "#         optimizer.step()\n",
    "#         running_loss += loss.item()\n",
    "#         total=total+1\n",
    "#         print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Total loss : {:.4f}'\n",
    "#             .format(epoch+1,epochs,i+1,total_step, loss.item(), running_loss))\n",
    "#     print(\"Total Loss : \" + str(running_loss/total))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.save(net.state_dict(), 'Saved Model\\\\RecogModelv1.pt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# torch.save(net, \"D:\\\\Projects\\\\Alzheimer's Research\\\\Saved Model\\\\RecogModel\")\n",
    "torch.save(net.state_dict(), \"D:\\\\Projects\\\\Alzheimer's Research\\\\Saved Model\\\\RecogModel\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
